//
//  LYMineHeaderView.m
//  artist
//
//  Created by Jay_Apple on 16/3/10.
//  Copyright © 2016年 viviyoo. All rights reserved.
//

#import "LYMineHeaderView.h"
#import "UIImage+Image.h"
#import "LYMineHeaderModel.h"
#import "UIImage+XMGExtension.h"
#import "LYBadgeBtn.h"
#import "LYMineHeaderItem.h"
#import "LYMineHeaderItemModel.h"

@interface LYMineHeaderView ()

@property (weak, nonatomic) UIView *badgeView;

@property (weak, nonatomic) IBOutlet UIImageView *bgImageView;


@property (weak, nonatomic) IBOutlet UIView *bottomView;

@property (weak, nonatomic) IBOutlet UIImageView *icon;

@property (weak, nonatomic) IBOutlet UILabel *userName;

@property (weak, nonatomic) IBOutlet UILabel *phone;

@property (weak, nonatomic) IBOutlet UIButton *loginBtn;

@property (assign, nonatomic) loginMode mode;

@end

@implementation LYMineHeaderView

- (void)awakeFromNib{

    //添加手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)];
    [self.icon addGestureRecognizer:tap];

}

#pragma mark - 手势处理
- (void)tap{

    if ([self.delegate respondsToSelector:@selector(headerIconOnclick:)]) {
        [self.delegate headerIconOnclick:self];
    }

}


#pragma mark - 重写

- (void)setModel:(LYMineHeaderModel *)model{

    _model = model;
    
    [self.icon setHeader:_model.icon];
    
    self.phone.text = _model.phone;
    self.userName.text = _model.userName;
    [self.bgImageView setImage:_model.bgImageName];
    [self setUpItemsWithItemModels:_model.items];
    self.mode = _model.mode;
    
    
}

- (void)setMode:(loginMode)mode{

    _mode = mode;
    
    switch (_mode) {
        case loginModeLoginIn:
            
            self.userName.hidden = NO;
            self.phone.hidden = NO;
            self.icon.hidden = NO;
            self.loginBtn.hidden = YES;
            
            break;
            
        case loginModeLoginOut:
            
            self.userName.hidden = YES;
            self.phone.hidden = YES;
            self.icon.hidden = YES;
            self.loginBtn.hidden = NO;

            break;
    }

}


- (void)setUpItemsWithItemModels:(NSArray<LYMineHeaderItemModel *> *)models{

    //移除原始按钮
    for (UIView *view in self.bottomView.subviews) {
        [view removeFromSuperview];
    }
    //新增按钮
    for (LYMineHeaderItemModel *model in models) {
        
        LYMineHeaderItem *item = [LYMineHeaderItem itemWithModel:model];
        [item addTarget:self action:@selector(bottomBtnOnclick:) forControlEvents:UIControlEventTouchUpInside];
        
        if (model != [models lastObject]) {
            
            UIView *seperator = [[UIView alloc] init];
            seperator.backgroundColor = [UIColor colorWithHexString:@"#999999" alpha:1.0];
            [self.bottomView addSubview:seperator];
            
        }
        
        [self.bottomView addSubview:item];
    }

}


- (void)bottomBtnOnclick:(LYMineHeaderItem *)item{

    if ([self.delegate respondsToSelector:@selector(header:bottomBtnOnclick:)]) {
        [self.delegate header:self bottomBtnOnclick:item.model];
    }
}

- (IBAction)loginBtnOnclick:(id)sender {
    
    if ([self.delegate respondsToSelector:@selector(headerLoginBtnOnclick:)]) {
        [self.delegate headerLoginBtnOnclick:self];
    }
}

- (void)layoutSubviews{

    [super layoutSubviews];
    
    NSUInteger count = self.model.items.count;
    
    CGFloat itemX;
    CGFloat itemY = 0;
    CGFloat itemW = LYScreenWidth / count;
    CGFloat itemH = self.bottomView.My_Height;
    
    CGFloat sepCenterX;
    CGFloat sepCenterY = self.bottomView.My_Height * 0.5;
    CGFloat sepW = 1;
    CGFloat sepH = 25;
    
    NSUInteger itemIndex = 0;
    NSUInteger seperatorIndex = 1;
    
    for (UIView *item in self.bottomView.subviews) {
        
        if ([item isMemberOfClass:[LYMineHeaderItem class]]) {
            
            itemX = itemIndex * itemW;
            
            item.frame = CGRectMake(itemX, itemY, itemW, itemH);
            
            itemIndex++;

        }else if ([item isMemberOfClass:[UIView class]]) {
            
            [self.bottomView bringSubviewToFront:item];
            
            sepCenterX = seperatorIndex * itemW;
            
            item.My_Width = sepW;
            item.My_Height = sepH;
            item.My_CenterY = sepCenterY;
            item.My_CenterX = sepCenterX;
            
            seperatorIndex++;
            
        }

    }
    

}

@end
